﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using exsportance.Silverlight.Common.Utils;

namespace exsportance.Silverlight.Validate
{
    /// <summary>
    /// A single validation item
    /// </summary>
    public class ValidationItem : IValidation
    {
        #region members
        private IValidator _validator;
        private FrameworkElement _control;
        private DependencyProperty _property;
        private object _value;
        private FrameworkElement _messageControl;
        private DependencyProperty _messageControlProperty;
        #endregion

        #region properties
        /// <summary>
        /// The item's name
        /// </summary>
        public string Name { get; set; }
        #endregion

        #region constructor
        /// <summary>
        /// Create a new ValidationItem
        /// </summary>
        /// <param name="validator"></param>
        /// <param name="control"></param>
        /// <param name="property"></param>
        public ValidationItem(IValidator validator, FrameworkElement control, DependencyProperty property)
        {
            if (validator == null)
                throw new ArgumentNullException("validator");

            if (control == null)
                throw new ArgumentNullException("control");

            if (property == null)
                throw new ArgumentNullException("property");

            _validator = validator;
            _control = control;
            _property = property;
            Name = "";
        }

        /// <summary>
        /// Create a new ValidationItem
        /// </summary>
        /// <param name="name"></param>
        /// <param name="validator"></param>
        /// <param name="control"></param>
        /// <param name="property"></param>
        public ValidationItem(string name, IValidator validator, FrameworkElement control, DependencyProperty property)
            : this(validator, control, property)
        {
            Name = name;
        }

        /// <summary>
        /// Create a new ValidationItem
        /// </summary>
        /// <param name="validator"></param>
        /// <param name="value"></param>
        public ValidationItem(IValidator validator, object value)
        {
            if (validator == null)
                throw new ArgumentNullException("validator");

            if (value == null)
                throw new ArgumentNullException("value");

            _validator = validator;
            _value = value;
            Name = "";
        }

        /// <summary>
        /// Create a new ValidationItem
        /// </summary>
        /// <param name="name"></param>
        /// <param name="validator"></param>
        /// <param name="value"></param>
        public ValidationItem(string name, IValidator validator, object value)
            : this(validator, value)
        {
            Name = name;
        }
        #endregion

        #region public methods
        /// <summary>
        /// Execute the validator
        /// </summary>
        /// <returns></returns>
        public ValidationResult Validate()
        {
            return Validate(true);
        }

        /// <summary>
        /// Execute the validator
        /// </summary>
        /// <param name="allowMessages">Are messages allowed</param>
        /// <returns></returns>
        public ValidationResult Validate(bool allowMessages)
        {
            ValidationResult result = new ValidationResult();

            object value = _value;

            if (_control != null)
                value = getControlValue();

            result.ValueProvided = !value.IsNullOrEmpty();
            result.Result = _validator.Validate(value, allowMessages);
            result.Messages.AddRange(_validator.Messages, _control);

            if (allowMessages)
                showMessages(result);

            return result;
        }

        /// <summary>
        /// Execute the validator
        /// </summary>
        /// <returns></returns>
        public bool IsValid()
        {
            return Validate(false).Result;
        }

        /// <summary>
        /// Set the control where messages are shown
        /// </summary>
        /// <param name="control"></param>
        /// <param name="contentProperty"></param>
        public void SetMessageControl(FrameworkElement control, DependencyProperty contentProperty)
        {
            if (control == null)
                throw new ArgumentNullException("control");

            if (contentProperty == null)
                throw new ArgumentNullException("contentProperty");

            _messageControl = control;
            _messageControlProperty = contentProperty;
        }
        #endregion

        #region private methods
        private object getControlValue()
        {
            return _control.GetValue(_property);
        }

        /// <summary>
        /// Shows the messages if a message control is provided
        /// </summary>
        private void showMessages(ValidationResult result)
        {
            if (_messageControl != null)
            {
                string messages = result.GetMessages();

                _messageControl.SetValue(_messageControlProperty, messages);
            }
        }
        #endregion
    }
}
